//
//  SettingScene.m
//  ShoutBoss
//
//  Created by Yin supmarboro on 09-6-24.
//  Copyright 2009 JW. All rights reserved.
//

#import "SettingScene.h"
#import "SettingBackGround.h"
#import "MainMenuScene.h"


@implementation SettingScene

-(id) init
{
	self = [super init];
	if(self != nil){
		//LogoScene 初始化工作
		SettingLayer * logoLayer =[SettingLayer node];	
		[self addChild: logoLayer];
	}
	
	return self;
}

@end

@implementation SettingLayer
-(id) init
{
	if( ( self=[super init] ))
	{
		//启动Touch
		isTouchEnabled = YES;
		
		//LogoLayer 初始化工作
		CGSize s = [[Director sharedDirector] winSize];
		
		SettingBackGround* background = [SettingBackGround node];
		[self addChild:background];
		[background setPosition: ccp(s.width/2, s.height/2)];
		
		//标签
		label1 = [Label labelWithString:@"Setting View" fontName:@"Courier" fontSize:32];
		label1.position = ccp(480-120, 320-32);
		[self addChild:label1];
		
		//Slider
		ctrlLayer = [SettingSliderUILayer node];
		[self addChild:ctrlLayer];
		
		//MainMenu
			//1. BG Sound
		[MenuItemFont setFontName: @"American Typewriter"];
		[MenuItemFont setFontSize:18];
		MenuItemFont *title1 = [MenuItemFont itemFromString: @"BG Music"];
		[title1 setIsEnabled:NO];
		[MenuItemFont setFontName: @"Marker Felt"];
		[MenuItemFont setFontSize:34];
		MenuItemToggle *item1 = [MenuItemToggle itemWithTarget:self selector:@selector(menuBGCallback:) items:
								 [MenuItemFont itemFromString: @"Off"],
								 [MenuItemFont itemFromString: @"On"],
								 nil];
			//2. Action Sound
		[MenuItemFont setFontName: @"American Typewriter"];
		[MenuItemFont setFontSize:18];
		MenuItemFont *title2 = [MenuItemFont itemFromString: @"Action Music"];
		[title2 setIsEnabled:NO];
		[MenuItemFont setFontName: @"Marker Felt"];
		[MenuItemFont setFontSize:34];
		MenuItemToggle *item2 = [MenuItemToggle itemWithTarget:self selector:@selector(menuActionCallback:) items:
								 [MenuItemFont itemFromString: @"Off"],
								 [MenuItemFont itemFromString: @"On"],
								 nil];
			//3.Title
		[MenuItemFont setFontName: @"American Typewriter"];
		[MenuItemFont setFontSize:18];
		MenuItemFont *title3 = [MenuItemFont itemFromString: @"Sound"];
		[title3 setIsEnabled:NO];
		
		Menu *menu = [Menu menuWithItems:
		title1, title2, item1, item2, title3, nil];
		
		[menu alignItemsInColumns:
		 [NSNumber numberWithUnsignedInt:2],
		 [NSNumber numberWithUnsignedInt:2],
		 [NSNumber numberWithUnsignedInt:1],
		 nil
		 ]; // 2 + 2 + 2 + 2 + 1 = total count of 9.
		
		[self addChild: menu];
		
		//RetunMenu
		[MenuItemFont setFontName: @"Marker Felt"];
		[MenuItemFont setFontSize:34];
		MenuItem *itemReturn = [MenuItemFont itemFromString:@"Done" target:self selector:@selector(menuReturnCallBack:)];		
		Menu *menuReturn = [Menu menuWithItems:
						   itemReturn, nil];
		[menuReturn setPosition:ccp(480-48, 32)];
		
		[self addChild: menuReturn];
	}
	return self;
}

-(void) menuBGCallback: (id) sender
{
	if ([[SettingInfo sharedManager] GetBGMusic] == YES){
		[[SettingInfo sharedManager] SetBGMusic:FALSE];
	}
	else{
		[[SettingInfo sharedManager] SetBGMusic:YES];
	}
}

-(void) menuActionCallback: (id) sender
{
	if ([[SettingInfo sharedManager] GetActionMusic] == YES){
		[[SettingInfo sharedManager] SetActionMusic:FALSE];
	}
	else{
		[[SettingInfo sharedManager] SetActionMusic:YES];
	}
}

-(void) menuReturnCallBack: (id) sender
{
	//保存音量大小
	[[SettingInfo sharedManager] SetSound:[ctrlLayer GetSliderValue]];
	// 切换到MainMenu
	MainMenuScene* mainMenuScene = [MainMenuScene node];
	[[Director sharedDirector] replaceScene:mainMenuScene];
}

- (void) dealloc
{
	[super dealloc];
}
@end

@implementation SettingSliderUILayer
-(id) init
{	
	self = [super init]; 
	
	if (self != nil){
		//very important, need set same size or larger than subviews.
		[self setFrame: CGRectMake(0, 0, 201, 51)];
		//[self setPosition: ccp(320,170)]; 
		[self setPosition: ccp(100, 245)]; 
		
		/*UITextField *view = [[UITextField alloc] initWithFrame: CGRectMake(100, 228, 220, 28)];
		 view.transform = CGAffineTransformMakeRotation(M_PI * (90.0 / 180.0)); // rotate for landscape
		 view.backgroundColor = [UIColor clearColor];
		 view.borderStyle = UITextBorderStyleRoundedRect;
		 view.delegate = self; // set this layer as the UITextFieldDelegate
		 view.returnKeyType = UIReturnKeyDone; // add the 'done' key to the keyboard
		 view.autocorrectionType = UITextAutocorrectionTypeNo; // switch of auto correction
		 view.autocapitalizationType = UITextAutocapitalizationTypeWords;
		 
		 UITextView *view =  [[UITextView alloc] initWithFrame:CGRectMake(0,0, 280, 260)]; 
		 view.font = [UIFont systemFontOfSize:12.0]; 
		 view.textColor = [UIColor whiteColor];
		 view.backgroundColor = [UIColor clearColor];
		 view.textAlignment = UITextAlignmentLeft;
		 //view.editable = FALSE;
		 view.text = NSLocalizedStringFromTable(@"AboutContent",@"resource",nil);
		 [self addChild: view];*/
		
		contentSlider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
		
		[contentSlider setTransform:CGAffineTransformMakeRotation(1.569)];
		//[contentSlider setTouchInside:YES];
		//[contentSlider setMultipleTouchEnabled:YES];
		contentSlider.value = 0;
		contentSlider.selected = YES;
		contentSlider.backgroundColor = [UIColor clearColor];
		contentSlider.minimumValue = 0;
		contentSlider.maximumValue = 100;
		
		[self addChild: contentSlider];	
	}
	return self;
} 

-(int)GetSliderValue
{
	return contentSlider.value;
}

-(void) dealloc
{
	//need manual release subviews
	[contentSlider removeFromSuperview];
	[contentSlider release];
	[super dealloc];
} 

@end
